package com.school.food.controller.admin;

import com.github.pagehelper.PageInfo;
import com.school.food.domain.Config;
import com.school.food.service.ConfigService;
import com.school.support.base.AdminBaseController;
import com.school.support.base.Page;
import com.school.support.example.Example;
import com.school.support.example.ExampleType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin/config")
public class MConfigController extends AdminBaseController {

    @Autowired
    private ConfigService configService;

    @RequestMapping("list")
    public String list(Model model, Page page, Config config) {
        Example example = Example.getInstance()
                .addParam("name", config.getName(), ExampleType.Operation.LIKE);
        PageInfo<Config> pageInfo = configService.selectByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "admin/config/list";
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Config config = configService.selectByPK(id);
        model.addAttribute(config);
        return "admin/config/detail";
    }

    @RequestMapping("update")
    public String update(Config config) {
        configService.updateByPKSelective(config);
        return redirect("detail/" + config.getId());
    }
}
